<template>
  <view class="goods-item">
    <!-- item项的左侧 -->
    <view class="goods-item-left">
      <radio :checked="good.goods_state" color="#c00000" v-if="showRatio" @click="ratioClickHandler"></radio>
      <image :src="good.goods_small_logo || defaultPic " class="goods-pic"></image>
    </view>
    <!-- item项的右侧 -->
    <view class="goods-item-right">
      <!-- 商品名字 -->
      <view class="good-name">{{good.goods_name}}</view>
      <!-- 商品信息区域 -->
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price">￥{{good.goods_price | tofixed}}</view>
        <!-- 商品数量 -->
        <uni-number-box :min="1" background="#e7e7e7" :value="good.goods_count" v-if="showNum" @change="changeHandler"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    data() {
      return {
        // 默认的商品图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods: {
      // ratio点击事件处理函数
      ratioClickHandler() {
        this.$emit('ratio-change', {
          // 返回goods的值(id与state取反)
          goods_id: this.good.goods_id,
          // 商品最新的勾选状态
          goods_state: !this.good.goods_state
        })
      },
      // 监听numberBox组件的值改变事件
      changeHandler(val){
        this.$emit('num-change', {
          goods_id: this.good.goods_id,
          goods_count: +val,
        })
        console.log(val)
      }
    },
    props: {
      good: {
        type: Object,
        default: {}
      },
      // 该组件是否显示ratio
      showRatio: {
        type: Boolean,
        default: false
      },
      // 是否展示数量组件
      showNum:{
        type:Boolean,
        default:false
      }
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #b3b4be;
    
    
    .goods-item-left {
      margin-right: 5px;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-pic {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      .good-name {
        font-size: 13px;
      }

      .goods-info-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .goods-price {
          font-size: 16px;
          color: #c00000;
        }
      }
    }
  }
</style>
